<?php
/**
* This file belongs to the core webappkit package (kit)
* @link http://webappkit.net
* @package webappkit
* @subpackage debug
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/**
* static debuggings methods
*/
class debug {

	/**
	* returns a string telling the caller of the current function/method
	* @access public
	* @param int $skipLevels (default 1)
	* @return string 
	* @static
	*/
	function getCaller($skipLevels=0) {
		// skipping one more level : this call
		$skipLevels+=1;
		// getting caller
		$backtrace=debug_backtrace();
		// echo '<pre>'; var_dump($backtrace); echo '</pre>';
		if (!isset($backtrace[$skipLevels])) return '!!too much skipLevels!!';
		// defining caller string
		$caller='';
		$caller.=isset($backtrace[$skipLevels]['class'])?
			$backtrace[$skipLevels]['class'].$backtrace[$skipLevels]['type']
			:basename($backtrace[$skipLevels]['file']).': line '.$backtrace[$skipLevels]['line'].': ';
		$caller.=isset($backtrace[$skipLevels]['function'])?
			$backtrace[$skipLevels]['function'].'():':'';
		//$caller.='('.$backtrace[$skipLevels]['file'].')';
		return $caller;
	}/**/
	
	/**
	* returns a dump of the var
	* @static
	* @return string
	* @param mixed $var by reference the var to dump
	* @param string $mode text html
	* @param int $cut
	*/
	function varDump(&$var, $mode='html',$cut=50, $niv=1) {
		//echo $niv;
		// defining markup
		switch ($mode) {
			case 'html':
				$beforeType='';
				$afterType='';
				$beforeDetails='(';
				$afterDetails=')';
				$beforeValue=' <var>';
				$afterValue='</var>';
				$newline='<br />';
				$indent=''; for ($f=1; $f<=$niv; $f++) $indent.='. ';
				break;
			case 'text':
			default:
				$beforeType='';
				$afterType='';
				$beforeDetails='(';
				$afterDetails=')';
				$beforeValue=' [';
				$afterValue=']';
				$newline=chr(13).chr(10);
				$indent=''; for ($f=1; $f<$niv; $f++) $indent.=chr(11);
		}
		// analysing var, building and returning string
		$details='';
		$type=gettype($var);
		$value='';
		switch (true) {
			/*case is_numeric($var):
				$value=strval($var);
				break;*/
			case is_string($var):
				//$type='string';
				$details=strlen($var);
				$value=substr($var,0,$cut);
				$value=debug::xmlentities($value);
				break;
			case is_array($var):
				//$type='array';
				$details=count($var);
				foreach ($var as $key => $item)
					$value.=$newline.$indent.$key.' = '.debug::vardump($item,$mode,$cut,$niv+1);
				break;
			case is_object($var):
				$details=get_class($var);
				if ($parent=get_parent_class($var)) $details.=' extends '.$parent;
				$subvars=get_object_vars($var);
				foreach ($subvars as $key => $item) {
					// skipping vars starting by _
					if ($key[0]=='_') continue;
					$value.=$newline.$indent.$key.' = '.debug::vardump($item,$mode,$cut,$niv+1);
				}
				break;
			case is_bool($var):
				//$details=
				$details=$var?'true':'false';
				$value='';
				break;
			/*case is_null($var):
				
				break;*/
			default:
				//$type=gettype($var);
				$value=substr(strval($var),0,$cut);
		}
		// building and returning string
		$r=$beforeType.$type.$afterType
		.$beforeDetails.$details.$afterDetails
		.$beforeValue.$value.$afterValue;
		if ($niv==1) $r.=$newline;
		return $r;
	}/**/
	
	/**
	* replace xml special chars by their entity
	* @param string $text
	* @static
	* @return string
	*/
	function & xmlentities($str) {
		$str=str_replace('&','&amp;',$str);
		$str=str_replace('"','&quot;',$str);
		$str=str_replace("'",'&apos;',$str);
		$str=str_replace('<','&lt;',$str);
		$str=str_replace('>','&gt;',$str);
		return $str;
	}
}/**/
?>
